//
//  CategoryView.m
//  礼物
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "CategoryView.h"


@implementation CategoryView


-(void)setAllDic:(NSMutableDictionary *)allDic
{
    _allDic = allDic;
    self.menuScrollV = [[BaseScrollView alloc] initWithFrame:CGRectMake(0, 0, self.menu_width, self.frame.size.height)];
    self.menuScrollV.delegate = self;
    [self addSubview:self.menuScrollV];
    [self createButton];
    
    self.mainTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(self.menuScrollV.frame.size.width, 0, WIDTH - self.menu_width, self.frame.size.height) style:UITableViewStyleGrouped];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self addSubview:self.mainTableView];
    NSLog(@" === %f====,%f",WIDTH - self.menu_width,self.menu_width);
    
}
-(void)createButton
{
    NSArray *keyArray =  [self.allDic allKeys];
    self.menuScrollV.contentSize = CGSizeMake(self.menuScrollV.frame.size.width, self.menu_button_height *keyArray.count);
    for (int  i = 0; i<keyArray.count; i++) {
        BaseButton *menuButton = [BaseButton buttonWithType:UIButtonTypeCustom];
        menuButton.frame = CGRectMake(0, i*self.menu_button_height, self.menu_width, self.menu_button_height);
        [self.menuScrollV addSubview:menuButton];
        [menuButton setTitle:[keyArray objectAtIndex:i] forState:UIControlStateNormal];
        menuButton.tag = 10000+i;
        if (i == 0) {
            [menuButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            menuButton.titleLabel.font = [UIFont systemFontOfSize:18*HSCALE];
            menuButton.backgroundColor = [UIColor whiteColor];
            self.selectedButton = menuButton;
        }else
        {
            //NSLog(@"???");
            menuButton.titleLabel.font = [UIFont systemFontOfSize:15*HSCALE];
            [self changeColorForButton:menuButton red:0];
            menuButton.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:245/255.0 alpha:1.0];
        }
        [menuButton addTarget:self action:@selector(checkingAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
}
-(void)getCategoryMessage:(CategoryModel *)category
{
    [self.myDelegate sendDelegate:category];
}
-(void)checkingAction:(BaseButton *)button
{
    self.selectedButton = button;
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:button.tag - 10000];
//    [self.mainTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
   CGRect temp = [self.mainTableView rectForSection:(self.selectedButton.tag-10000)];
    [self.mainTableView setContentOffset:CGPointMake(temp.origin.x, temp.origin.y)];
    NSArray *keyArray = [self.allDic allKeys];
    
    for (int i = 0; i<keyArray.count; i++ ) {
        UIButton *button = (UIButton *) [self viewWithTag:10000+i ];
        button.titleLabel.font = [UIFont systemFontOfSize:15*HSCALE];
        [self changeColorForButton:button red:0];
        button.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:245/255.0 alpha:1.0];
    }
     [self.menuScrollV scrollRectToVisible:CGRectMake(0, (self.selectedButton.tag - 10000)*self.menu_button_height -100, self.menuScrollV.frame.size.width, self.menuScrollV.frame.size.height) animated:YES];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:18*HSCALE];
    button.backgroundColor = [UIColor whiteColor];
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    
    return 10;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"y ===== %f",scrollView.contentOffset.y);
    if (scrollView == self.mainTableView) {
        if (scrollView.contentOffset.y > 0) {
            
            CGRect temp  = [self.mainTableView rectForSection:(self.selectedButton.tag-10000)];
//            NSLog(@"x= %f , y = %f , w = %f , h = %f",temp.origin.x,temp.origin.y,temp.size.width,temp.size.height);
            if (scrollView.contentOffset.y >=( temp.origin.y +temp.size.height)) {
                NSArray *keyArray = [self.allDic allKeys];
                
                for (int i = 0; i<keyArray.count; i++ ) {
                    UIButton *button = (UIButton *) [self viewWithTag:10000+i ];
                    button.titleLabel.font = [UIFont systemFontOfSize:15*HSCALE];
                   button.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:245/255.0 alpha:1.0];
                    [self changeColorForButton:button red:0];
                }
                
                
                BaseButton *button = (BaseButton *)[self viewWithTag:self.selectedButton.tag+1];
                [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                button.backgroundColor = [UIColor whiteColor];
                button.titleLabel.font = [UIFont systemFontOfSize:18*HSCALE];
                [self.menuScrollV scrollRectToVisible:CGRectMake(0, (self.selectedButton.tag - 10000)*self.menu_button_height -100, self.menuScrollV.frame.size.width, self.menuScrollV.frame.size.height) animated:YES];

                self.selectedButton = button;
                
            }else if (scrollView.contentOffset.y<(temp.origin.y )) {
                NSArray *keyArray = [self.allDic allKeys];
                
                
                for (int i = 0; i<keyArray.count; i++ ) {
                    UIButton *button = (UIButton *) [self viewWithTag:10000+i ];
                    button.titleLabel.font = [UIFont systemFontOfSize:15*HSCALE];
                    button.backgroundColor  = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:245/255.0 alpha:1.0];
                    [self changeColorForButton:button red:0];
                }
                
                BaseButton *button = (BaseButton *)[self viewWithTag:self.selectedButton.tag-1];
                [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:18*HSCALE];
                button.backgroundColor =[UIColor whiteColor];
                [self.menuScrollV scrollRectToVisible:CGRectMake(0, (self.selectedButton.tag - 10000)*self.menu_button_height -100, self.menuScrollV.frame.size.width, self.menuScrollV.frame.size.height) animated:YES];

                self.selectedButton = button;
                
            }
            
            
        }

    }
}

- (void)changeColorForButton:(UIButton *)btn red:(float)nRedPercent
{
    float value = [self lerp:nRedPercent min:0 max:212];
    [btn setTitleColor:RGBA(value,25,38,1) forState:UIControlStateNormal];
}
-(float)lerp:(float)percent min:(float)nMin max:(float)nMax
{
    float result = nMin;
    
    result = nMin + percent * (nMax - nMin);
    
    return result;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSArray *keyArray = [self.allDic allKeys];
    return keyArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifiet"];
    if (!cell) {
        cell = [[CategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
        cell.delegate = self;
    }
    NSArray *keyArray = [self.allDic allKeys];
    NSString *key = [keyArray objectAtIndex:indexPath.section];
    cell.categories =[self.allDic objectForKey:key];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *keyArray = [self.allDic allKeys];
    NSString *key = [keyArray objectAtIndex:indexPath.section];
    Categories *categories = [self.allDic objectForKey:key];
    CGRect temp;
    if (categories.subcategories.count % 3 == 0) {
        temp.size.height =categories.subcategories.count / 3 *(80*WSCALE +30*HSCALE+10*HSCALE)+15*HSCALE;
    }else{
        temp.size.height =(categories.subcategories.count / 3+1) *(80*WSCALE +30*HSCALE+10*HSCALE)+15*HSCALE;
    }

    return temp.size.height+20*HSCALE;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    NSArray *keyArray = [self.allDic allKeys];
    NSString *key  = [keyArray objectAtIndex: section];
    titleLabel.text = [NSString stringWithFormat:@"---- %@ ----",key];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    return (UIView *)titleLabel;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
@end
